using System;
using System.Collections.Generic;
using EventBehaviorTree;

namespace BuffSystem
{
    [Serializable]
    public class Buff
    {
        public string _name;//key in the _buff_Dic of gameManager
        public string _key;//key in the _buffEvent_Registry of target
        [NonSerialized] private BuffManager _owner;
        [NonSerialized] private BuffManager _target;
        private Container _rootNode;
        private List<FunctionComponent> _component_List = new List<FunctionComponent>();
        public Buff(string name) { _name = name; }
        public void SetRootNode(Container rootNode) { _rootNode = rootNode; }
        public FunctionComponent AddComponent(FunctionComponent component)
        {
            _component_List.Add(component);
            return component;
        }
        public void RegisterBuff(BuffManager owner, BuffManager target)
        {
            _owner = owner;
            _target = target;
            _key = _name + "." + _owner.name + "." + _target.name;
            foreach (FunctionComponent component in _component_List)
            {
                component.RegisterPrepare(this, _owner, _target);
            }
            _rootNode.Initialize(_target);
            _target.Register(_key, this);
            _rootNode.NodeEvent(NodeMessage.Start);//start the first available task
        }
        public void UnregisterBuff()
        {
            foreach (FunctionComponent component in _component_List) { component.Unregister(); }
        }
    }
}
